package com.wugui.datax.admin.controller;

import com.wugui.datatx.core.biz.model.ReturnT;
import com.wugui.datax.admin.entity.ColumnInfo;
import com.wugui.datax.admin.entity.DBInfo;
import com.wugui.datax.admin.entity.TableInfo;
import com.wugui.datax.admin.service.DBService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "数据仓库接口")
@RestController
@RequestMapping("/api/db")
public class DBController extends BaseController{
    @Resource
    private DBService dbService;

    @GetMapping("/list")
    @ApiOperation("数据库列表")
    public ReturnT<List<DBInfo>> list(){
        return new ReturnT<>(dbService.list());
    }

    @GetMapping("/tables")
    @ApiOperation("数据库中表列表")
    public ReturnT<List<TableInfo>> tableList(@RequestParam(required = true, defaultValue = "datax_web")String dbName){
        return new ReturnT<>(dbService.tableList(dbName));
    }

    // 接入方式：全部、数据库接入、实时接入、结构文本、数据开发、数据清洗
    // 状态：全部、正在执行、执行成功、执行失败
    @GetMapping("/search/tables")
    @ApiOperation("指定数据库中查询表列表")
    public ReturnT<List<TableInfo>> tableSearchList(@RequestParam(defaultValue = "datax_web")String dbName,
                                                    @RequestParam(required = false)String tableName,
                                                    @RequestParam(required = false,defaultValue = "0")String collectWay,
                                                    @RequestParam(required = false,defaultValue = "0")String status){
        return new ReturnT<>(dbService.tableSearchList(dbName,tableName,collectWay,status));
    }

    @GetMapping("/table/column")
    @ApiOperation("根据表名查询字段信息")
    public ReturnT<List<ColumnInfo>> tableColumnInfo(@RequestParam(defaultValue = "datax_web")String dbName,
                                                     @RequestParam()String tableName){
        return new ReturnT<>(dbService.tableColumnInfo(dbName,tableName));
    }

    @GetMapping("/table/data")
    @ApiOperation("根据表名查询表数据")
    public ReturnT<List<Object>> tableData(@RequestParam(defaultValue = "datax_web")String dbName,
                                                     @RequestParam String tableName,@RequestParam(defaultValue = "10") int nums){
        return new ReturnT<>(dbService.tableData(dbName,tableName,nums));
    }

}
